package dbhandler;

import java.sql.*;

public class DBUtil {
    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/student";
    private static String user = "root";
    private static String password = "123456";

    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(Connection conn, PreparedStatement pst, ResultSet rs){
        try {
            conn.close();
        } catch (SQLException e) {
            System.out.println("数据库连接异常"+e.getMessage());
            e.printStackTrace();
        }

        try {
            pst.close();
        } catch (SQLException e) {
            System.out.println("关闭异常"+e.getMessage());
            e.printStackTrace();
        }

        try {
            rs.close();
        } catch (SQLException e) {
            System.out.println("关闭异常"+e.getMessage());
            e.printStackTrace();
        }
    }

}
